package org.example.threadtest;

class ResNumber {
    public int count = 0;
    public static ThreadLocal<Integer> threadLocal = new ThreadLocal<Integer>(){
        @Override
        protected Integer initialValue() {
            return 0;
        }
    };
    public String getNumber() {
        count = threadLocal.get()+1;
        threadLocal.set(count);
        return count+"";
    }

}

class ThreadLocalDemo extends Thread {
    private ResNumber resNumber;

    public ThreadLocalDemo(ResNumber resNumber) {
        this.resNumber = resNumber;
    }
    @Override
    public void run() {
        for (int i = 0; i < 3; i++) {
            System.out.println(getName()+",number:"+ resNumber.getNumber());
        }
    }
}

public class ThreadLocalDemoTest {
    public static void main(String[] args) {
        ResNumber resNumber = new ResNumber();
        ThreadLocalDemo t1 = new ThreadLocalDemo(resNumber);
        ThreadLocalDemo t2 = new ThreadLocalDemo(resNumber);
        ThreadLocalDemo t3 = new ThreadLocalDemo(resNumber);
        t1.start();
        t2.start();
        t3.start();
    }
}
